<script setup>
import { ref } from 'vue'
import { getGoodsListAPI } from '@/api/goods'
import { useInitTable } from '@/composables/useCommon'
const dialogVisible = ref(false)
const {
  handleSelectionChange,
  multipleTableRef,
  searchForm,
  tableData,
  loading,
  total,
  currentPage,
  getData,
  multiSelectionIds,
} = useInitTable(
  {
    searchForm: {
      title: '',
      tab: 'all',
      category_id: null
    },
    getList: getGoodsListAPI,
    onGetListSuccess: (res) => {
      tableData.value = res.list
      total.value = res.totalCount
    },
  }
)


const close = () => {
  dialogVisible.value = false
}
const callbackFunction = ref(null)
const open = (callback = null) => {
  if (callback) {
    callbackFunction.value = callback
  }
  dialogVisible.value = true
}
const submit = () => {
  if (typeof callbackFunction.value === 'function') {
    callbackFunction.value(multiSelectionIds.value)
  }
  close()
}
defineExpose({
  open
})
</script>
<template>
  <el-dialog title="商品选择" v-model="dialogVisible" width="80%" destroy-on-close>
    <el-table ref="multipleTableRef" @selection-change="handleSelectionChange" class="mt-3" v-loading="loading"
      :data="tableData" border stripe style="width: 100%;" height="300px">
      <el-table-column type="selection" width="55"></el-table-column>

      <el-table-column label="商品">
        <template #default="scope">
          <div class="flex ">
            <el-image class="mr-3 rounded" :src="scope.row.cover" fit="cover" :lazy="true"
              style="width: 50px; height: 50px;"></el-image>
            <div class="flex-1">
              <p>{{ scope.row.title }}</p>
              <p class="text-gray-400 text-xs mb-1">分类:{{ scope.row.category ? scope.row.category.name : '未分类' }}
              </p>
              <p class="text-gray-400 text-xs">创建时间:{{ scope.row.create_time }}</p>
            </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="总库存" prop="stock" width="90" align="center"></el-table-column>
      <el-table-column label="价格(元)" width="150" align="center">
        <template #default="{ row }">
          <span class="text-rose-500">￥{{ row.min_price }}</span>
          <el-divider direction="vertical">
            <span class="text-gray-500 text-xs">￥{{ row.min_price }}</span>
          </el-divider>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination class="flex justify-center mt-auto-3" background layout="prev, pager, next" :total="total"
      :current-page="currentPage" @current-change="getData" />
    <template #footer>
      <span>
        <el-button @click="close">取消</el-button>
        <el-button type="primary" @click="submit">确认</el-button>
      </span>
    </template>
  </el-dialog>

</template>